//
//  CMJCommonController.m
//  CMJWeibo
//
//  Created by BH on 15/12/8.
//
//

#import "CMJCommonController.h"
#import "CMJCommonGroup.h"
#import "CMJCommonCheckGroup.h"
#import "CMJCommon.h"
#import "CMJCommonCell.h"
#import "CMJCommonItem.h"
#import "UIView+MJ.h"

@interface CMJCommonController ()

@property(nonatomic,strong)NSMutableArray *groups;

@end

@implementation CMJCommonController

-(NSArray *)groups
{
    if (!_groups) {
        self.groups = [NSMutableArray array];
    }
    return _groups;
}

-(CMJCommonGroup *)addGroup
{
    CMJCommonGroup *group = [CMJCommonGroup group];
    [self.groups addObject:group];
    return group;
}

-(CMJCommonCheckGroup *)addCheckGroup
{
    CMJCommonCheckGroup *group = [CMJCommonCheckGroup group];
    [self.groups addObject:group];
    return group;
}

-(id)groupInSection:(NSInteger)section
{
    return self.groups[section];
}

#pragma mark - 初始化
// init 内部默认会调用 initWithNibName
-(instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupTable];
}

/**
 *  设置tableview属性
 */
-(void)setupTable
{
    //去除分割线
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    //设置背景色
    self.tableView.backgroundView = nil;
    self.tableView.backgroundColor = GlobalBGColor;
    
    //设置节头结尾高度
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0;
    
    if (iOS7) {
        //增加滚动区域(内容)
        self.tableView.contentInset = UIEdgeInsetsMake(-25, 0, 0, 0);
    }
}

#pragma mark - tableview datasource & delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CMJCommonCell *cell = [CMJCommonCell cellWithTableView:tableView];
    CMJCommonGroup *group = self.groups[indexPath.section];
    cell.item = group.items[indexPath.row];  // 传递cell显示的模型数据
    cell.indexPath = indexPath;  //cell所在行
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CMJCommonGroup *group = self.groups[section];
    return group.items.count;
}

-(NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    CMJCommonGroup *group = self.groups[section];
    return group.footer;
}

-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CMJCommonGroup *group = self.groups[section];
    return group.header;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //取出这行对应的模型
    CMJCommonGroup *group = self.groups[indexPath.section];
    CMJCommonItem *item = group.items[indexPath.row];
    
    //判断有没有设置目标控制器
    if (item.destVc) {
        UIViewController *vc = [[item.destVc alloc] init];
        vc.title = item.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断有没有想要执行的操作(block)
    if (item.operation) {
        item.operation();
    }
}

@end
